package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.listcache.UserInfoCache;
import com.threeonemeeting.model.UserInfo;

public class UserInfoListAdapter extends ArrayAdapter<UserInfo> {

	private ArrayList<UserInfo> mAccountItems;

	public UserInfoListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setAccountList(ArrayList<UserInfo> accountList) {
		mAccountItems = accountList;
		notifyDataSetChanged();
	}

	public ArrayList<UserInfo> getAccountList() {
		return mAccountItems;
	}

	@Override
	public void add(UserInfo accountInfo) {
		if (mAccountItems == null)
			mAccountItems = new ArrayList<UserInfo>();

		mAccountItems.add(accountInfo);
	}

	public void clear() {
		if (mAccountItems != null) {
			mAccountItems.clear();
		}
	}

	@Override
	public int getCount() {
		if (mAccountItems == null)
			return 0;

		return mAccountItems.size();
	}

	@Override
	public UserInfo getItem(int position) {
		return mAccountItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();

		UserInfo info = getItem(position);

		View rowView = convertView;
		UserInfoCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.lvi_account_item, null);

			viewCache = new UserInfoCache(rowView, info.strNickName,
					info.strPassword);

			rowView.setTag(viewCache);
		} else {
			viewCache = (UserInfoCache) rowView.getTag();
		}

		TextView nameView = viewCache.getNameView();
		nameView.setText(info.strNickName);

		TextView emailView = viewCache.getEmailView();
		emailView.setText(info.strEmail);

		ImageView selectedView = viewCache.getSelectedImageView();
		selectedView.setVisibility(View.GONE);

		if (DataCenter.getInstance().currentLoginUserInfo.nUserId == info.nUserId) {
			selectedView.setVisibility(View.VISIBLE);
		}

		return rowView;
	}
}
